﻿using System;
using System.Web.UI.WebControls;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;

namespace SARS.Course.Group
{
    public partial class Group : System.Web.UI.Page
    {
        IGroupBO groupBiz = new GroupBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            if (Session["Roles"] == null || RoleType.Coordinator != (RoleType)Session["Roles"])
            {
                Response.Write("<script language='javascript'>window.alert('Your role is not allow to access this page.');" +
                               "window.location='../../Account/Login.aspx';</script>");

            }
        }

        protected void ucProgramCourse_OnSelectedCourseChanged()
        {
            BindingGridView();
        }

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            LinkButton btnEdit = sender as LinkButton;
            if (btnEdit != null)
            {
                GridViewRow row = (GridViewRow)btnEdit.NamingContainer;

                string id = row.Cells[0].Text;
                Response.Redirect("GroupEdit.aspx?gid=" + id);
            }
        }

        protected void btnSelect_OnClick(object sender, EventArgs e)
        {
            LinkButton btnSelect = sender as LinkButton;
            if (btnSelect != null)
            {
                GridViewRow row = (GridViewRow)btnSelect.NamingContainer;

                string id = row.Cells[0].Text;
                Response.Redirect("GroupDetails.aspx?gid=" + id);
            }
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            LinkButton btnDel = sender as LinkButton;
            if (btnDel != null)
            {
                GridViewRow row = (GridViewRow)btnDel.NamingContainer;

                string id = row.Cells[0].Text;
                groupBiz.RemoveGroup(Int32.Parse(id));

                BindingGridView();
            }
        }

        private void BindingGridView()
        {
            groupList.DataSource = groupBiz.RetriveAllGroupByCourseId(ucProgramCourse.SelectedCourseValue);
            groupList.DataBind();
        }

        protected void btnCreateLink_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateGroup.aspx");
        }
    }
}